<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2017/11/23 0023

 * Time: 15:15

 */



namespace app\admin\controller;

use think\Db;

use think\Controller;





class Zzbj extends Bases

{



    public function _initialize(){

        if(!session('id')){

            $this->redirect('/admin/index/index');

        }

    }



    public function index(){



        $where['sign']="pc";
        $where['is_del']=0;
        $data = Db::name('zzbj')->where($where)->order('id desc')->select();

        $this->assign('data',$data);



        return $this->fetch();

    }

    //按钮状态

    public function start(){

//        $is_recom = (int)input('post.is_recom');

        $id = (int)input('post.id');

        if($id){

            $info=Db::name('psjd')->where('id',$id)->find();

            if ($info['is_recom']==1){

                $data['is_recom']=0;

            }else{

                $data['is_recom']=1;

            }

            $result = Db::name('psjd')->where('id',$id)->update($data);

            if($result){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }

        }else{

            return json(['code'=>1,'msg'=>'数据异常']);

        }

    }



    public function add_cg(){

        $info=Db::name('psjdclass')->where('show',1)->select();

        $this->assign('info',$info);

        return $this->fetch();

    }

    public function edit_cg(){

        $id = input('param.id');

        if($id){
            $where['id'] = $id;
            $data =Db::name('zzbj')->where($where)->find();
        
            $this->assign('data',$data);


        }

        return $this->fetch();

    }

    //分类管理

    public function vclass(){

//            $where['id']=0;

//            $where['sign']="pc";

        $data = Db::name('psjdclass')->where('show',1)->order('class_id desc')->select();

        $this->assign('data',$data);



        return $this->fetch();

    }

//删除

    public function jddel(){

        $id = intval(input('post.id'));

        if($id){

            $res = Db::name('psjdclass')->where('class_id',$id)->delete();

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }



    public function add_vclass(){

        $id= intval(input('param.id'));

        if(request()->isAjax()){

            $data=[

                'name'=>input('param.name'),

                'show'=>intval(input('param.show'))

            ];

            $id= intval(input('param.id'));

            if($id> 0){

                $result = Db::name('psjdclass')->where('class_id',$id)->update($data);

            }else{

                $result = Db::name('psjdclass')->insert($data);

            }

            if($result !==false){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }



        }

        if($id){

            $data = Db::name('psjdclass')->where('class_id',$id)->find();

            $this->assign('data',$data);

        }

        return $this->fetch();

    }

    //确认提交

    public function addcg(){

        $id = input('post.id');

        $array_works_pic= isset($_POST['works_pic'])?implode(',',$_POST['works_pic']):'';

        $data=[

            'title'=>input('post.title'),

            'url'=>input('post.url'),
            'is_show'=>input('post.is_show'),
            'sign'=>"pc",

        ];

        $file_main_img = request()->file('main_img');

        $file_vice_img = request()->file('vice_img');

        if($file_main_img){

            $file_info_main_img = $file_main_img->move('./uploads/cg/');

            if($file_info_main_img){

                $data['main_img'] = '/uploads/cg/'.$file_info_main_img->getSaveName();

            }else{

                return json(['code'=>0,'msg'=>$file_main_img->getError()]);

            }

        }

        if($file_vice_img){

            $file_info_vice_img = $file_vice_img->move('./uploads/cg/');

            if($file_info_vice_img){

                $data['vice_img'] = '/uploads/cg/'.$file_info_vice_img->getSaveName();

            }else{

                return json(['code'=>0,'msg'=>$file_vice_img->getError()]);

            }

        }

        if($id){

            if($file_main_img){

                $old_img = Db::name('zzbj')->where('id',$id)->value('main_img');

                if($old_img){



                    unlink('.'.str_replace('\\','/',$old_img));

                }

            }

            $data['update_time']=time();

            $result = DB::name('zzbj')->where('id',$id)->update($data);

        }else{

            $data['create_time']=time();

            $result = Db::name('zzbj')->insert($data);

        }

        if($result !==false){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }



    //删除

    public function cgdel(){

        $id = intval(input('post.id'));

        if($id){

            $res = Db::name('zzbj')->where('id',$id)->update(['is_del'=>1]);

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }

    //改变排序

    public function ChangecgSort(){

        $sort = (int)input('post.sort');

        $id = (int)input('post.id');

        if($id){

            $data['sort']=$sort;

            $result = Db::name('psjd')->where('id',$id)->update($data);

            if($result){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }

        }else{

            return json(['code'=>1,'msg'=>'数据异常']);

        }



    }









}